package com.supermap.application.controller;

import com.supermap.application.service.IndustryChainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import net.sf.json.JSONObject;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 产业链接口
 * 迁移产业地图代码
 *
 */
@Controller
@RequestMapping(value = "/IndustryAnalysis")
public class IndustryChainController {

    @Autowired
    IndustryChainService industryChainService;

    /*
     *
     *Industrial chain 产业链
     * */
    //产业链     行业列表查询服务接口
    @RequestMapping(value = "/findIndustroyListes",method = RequestMethod.POST)
    @ResponseBody
    public String findIndustroyListes(){
        ArrayList<String> arrKeys1 = new ArrayList<String>();
        arrKeys1.add("pcode");
        arrKeys1.add("code");
        arrKeys1.add("text");
        String sql = "select A00012 as pcode,A00011 as code,INDUSTRY_NAME as text from t_industry_data order by pcode,code";
        List<Object> data = industryChainService.queryBySql(sql);
        LinkedHashMap mapRet = new LinkedHashMap();
        industryChainService.findIndustryAnalysis( mapRet, data, "data" ,arrKeys1 );
        JSONObject jsonObject=JSONObject.fromObject(mapRet);
        String strContent = jsonObject.toString();
        return strContent;
    }



    @RequestMapping(value = "/findData",method = RequestMethod.POST)
    @ResponseBody
    public String findData(@RequestParam("areacode")String areacode, @RequestParam("arealevel")String arealevel,
                           @RequestParam("hycode")String hycode, @RequestParam("areatype")String areatype){
        //          System.out.println(areacode);
        int arealevel2 = Integer.parseInt(arealevel);
        int areatype2 = Integer.parseInt(areatype);
        int codelen = areacode.length();
        String AreaCode_FN = "";
        if (areatype2 == 1)
        {
            codelen = arealevel2 * 2;
            if (arealevel2 == 4)
            {
                codelen = 9;
            }
            if (arealevel2 == 5 || arealevel2 == 6)
            {
                codelen = 12;
            }
            AreaCode_FN = "B00005";
        }
        else if (areatype2 == 2)
        {
            AreaCode_FN = "FUNCTION_CODE";
        }
        else if (areatype2 == 3)
        {
            AreaCode_FN = "PARK_CODE";
        }
        String strSql = "";
        if (hycode.length() == 1) {
            strSql = "select SMX,SMY,1 as QYCount,B00132,B00058,B00059,B00060,B04084 from t_base_data where " + AreaCode_FN + " like '" + areacode + "%' and A00011X ='" + hycode + "'";
        }
        else {
            strSql = "select SMX,SMY,1 as QYCount,B00132,B00058,B00059,B00060,B04084 from t_base_data where " + AreaCode_FN + " like '" + areacode + "%' and A00011 like'" + hycode + "%'";
        }
        List<Object> data = industryChainService.queryBySql(strSql);
        String sql = "select S00001,S00002 from t_industry_type where A00011 = '" + hycode + "'";
        List<Object> data_sxy = industryChainService.queryBySql(sql);
        ArrayList<String> arrKeys = new ArrayList<String>();
        LinkedHashMap mapRet = new LinkedHashMap();
        arrKeys.add("SMX");
        arrKeys.add("SMY");
        arrKeys.add("QYCount");
        arrKeys.add("B00132");
        arrKeys.add("B00058");
        arrKeys.add("B00059");
        arrKeys.add("B00060");
        arrKeys.add("B04084");
        industryChainService.findIndustryAnalysis( mapRet, data, "data" ,arrKeys );

        ArrayList<String> arrKeys1 = new ArrayList<String>();
        arrKeys1.add("S00001");
        arrKeys1.add("S00002");
        industryChainService.findIndustryAnalysis( mapRet, data_sxy, "data_sxy" ,arrKeys1 );
        JSONObject jsonObject=JSONObject.fromObject(mapRet);
        String strContent = jsonObject.toString();
        return strContent;
    }



    //产业链     上游行业信息查询服务接口
    @RequestMapping(value = "/findUpIndustroy",method = RequestMethod.POST)
    @ResponseBody       //     3201     3    2221，2922，2924，2928，3311
    public String findUpIndustroy(@RequestParam("areacode") String areacode,@RequestParam("arealevel")String arealevel,
                                  @RequestParam("sycode") String sycode,@RequestParam("areatype")String areatype){


        String sycode2 = sycode.replace("\n","");
//        String strCode = sycode2.replace(",","','");

        int arealevel2 = Integer.parseInt(arealevel);
        int areatype2 = Integer.parseInt(areatype);
        int codelen = areacode.length();
        String AreaCode_FN = "";
        if (areatype2 == 1)
        {
            codelen = arealevel2 * 2;
            if (arealevel2 == 4)
            {
                codelen = 9;
            }
            if (arealevel2 == 5 || arealevel2 == 6)
            {
                codelen = 12;
            }
            AreaCode_FN = "B00005";
        }
        else if (areatype2 == 2)
        {
            AreaCode_FN = "FUNCTION_CODE";
        }
        else if (areatype2 == 3)
        {
            AreaCode_FN = "PARK_CODE";
        }
        String strSql="";
        String[] strRetValue2 = sycode2.split(",");
        for (int i = 0; i < strRetValue2.length; i++) {
            if (strRetValue2[i] != "") {
                strSql += " or A00011 like '" + strRetValue2[i] + "%'";
//396，397，3990，4011，3562，3391，3322，2641
            }
        }
        if (strSql != "") {
            strSql = " and (" + strSql.substring(4) + ")";
        }
        String sql ="select SMX,SMY,1 as QYCount,B00132,B00058,B00059,B00060,B04084 from t_base_data where  "+ AreaCode_FN +" like '" + areacode + "%'"+strSql;

        List<Object> data = industryChainService.queryBySql(sql);
        ArrayList<String> arrKeys = new ArrayList<String>();
        arrKeys.add("SMX");
        arrKeys.add("SMY");
        arrKeys.add("QYCount");
        arrKeys.add("B00132");
        arrKeys.add("B00058");
        arrKeys.add("B00059");
        arrKeys.add("B00060");
        arrKeys.add("B04084");
        LinkedHashMap mapRet = new LinkedHashMap();
        industryChainService.findIndustryAnalysis( mapRet, data, "data" ,arrKeys );
        JSONObject jsonObject=JSONObject.fromObject(mapRet);
        String strContent = jsonObject.toString();
        return strContent;
    }



    //产业链     下游行业信息查询服务接口
    @RequestMapping(value = "/findDownIndustroy",method = RequestMethod.POST)
    @ResponseBody
    public String findDownIndustroy(@RequestParam("areacode") String areacode,@RequestParam("arealevel")String arealevel,
                                    @RequestParam("xycode") String xycode,@RequestParam("areatype")String areatype){
        ArrayList<String> arrKeys = new ArrayList<String>();
        arrKeys.add("SMX");
        arrKeys.add("SMY");
        arrKeys.add("QYCount");
        arrKeys.add("B00132");
        arrKeys.add("B00058");
        arrKeys.add("B00059");
        arrKeys.add("B00060");
        arrKeys.add("B04084");
        int arealevel2=Integer.parseInt(arealevel);
        String xycode2 =xycode.replace("\n","");
        int areatype2 = Integer.parseInt(areatype);
        int codelen = areacode.length();
        String AreaCode_FN = "";
        if (areatype2 == 1) {
            codelen = arealevel2 * 2;
            if (arealevel2 == 4) {
                codelen = 9;
            }
            if (arealevel2 == 5 || arealevel2 == 6) {
                codelen = 12;
            }
            AreaCode_FN = "B00005";
        }
        else if (areatype2 == 2) {
            AreaCode_FN = "FUNCTION_CODE";
        }
        else if (areatype2 == 3) {
            AreaCode_FN = "PARK_CODE";
        }
        String strSql="";
        String[] strRetValue2 = xycode2.split(",");
        for (int i = 0; i < strRetValue2.length; i++) {
            if (strRetValue2[i] != "") {
                strSql += " or A00011 like '" + strRetValue2[i] + "%'";
//396，397，3990，4011，3562，3391，3322，2641
            }
        }
        if (strSql != "") {
            strSql = " and (" + strSql.substring(4) + ")";
        }
        String sql ="select SMX,SMY,1 as QYCount,B00132,B00058,B00059,B00060,B04084 from t_base_data where "+ AreaCode_FN +" like '" + areacode + "%'"+strSql;
        List<Object> data = industryChainService.queryBySql(sql);
        LinkedHashMap mapRet = new LinkedHashMap();
        industryChainService.findIndustryAnalysis( mapRet, data, "data" ,arrKeys );
        JSONObject jsonObject=JSONObject.fromObject(mapRet);
        String strContent = jsonObject.toString();
        return strContent;
    }

}
